package com.demo.function.filter.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
@Component
public class XssFilter extends OncePerRequestFilter {

    private static final String MSG = "参数包含xss关键字";

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
        log.info("XssFilter开始过滤，请求url = " + request.getRequestURI());

        XssRequestWrapper wrapper = new XssRequestWrapper(request);
        if (wrapper.notPass()) {
            log.error(MSG);

            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write(MSG);
            return;
        }

        filterChain.doFilter(wrapper, response);
    }
}
